package com.example.layui.demo.controller;

import com.example.layui.demo.entity.*;
import com.example.layui.demo.entity.Article;
import com.example.layui.demo.mapper.ArticleMapper;
import com.example.layui.demo.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class MyController {


    @GetMapping("/abc")
    public String TestPage() {
        return "test";
    }

    @GetMapping("/")
    public String registerpage() {
        return "register";
    }

    @PostMapping("/register")
    @ResponseBody
    public Map<String, String> register(Customer customer) {
        System.out.println(customer);
        Map<String, String> map = new HashMap<>();
        map.put("msg", "你注册成功了");
        return map;
    }


    @GetMapping("/student")
    public String student() {
        return "stu";
    }

    @Autowired
    MyService myService;

    @GetMapping("/student1")
    @ResponseBody
    public Map<String, Result> student1() {
        Map<String, Result> map = new HashMap<>();
        Result result = myService.getCount();
        map.put("result", result);
        return map;
    }


    @GetMapping("/list")
    public String list() {
        return "list";
    }


    @GetMapping("/getList")
    @ResponseBody
    public MyResult getList(ArticleVO articleVo) {
        if (articleVo.getPage() == 0) {
            articleVo.setPage(1);
        }
        int start = (articleVo.getPage() - 1) * articleVo.getLimit();
        articleVo.setStart(start);
        Map<String, Object> map = new HashMap<>();
        List<Article> articleList = myService.getArticleList(articleVo);
        MyResult result = new MyResult(0, "请求成功", myService.getArticleCount(articleVo), articleList);
        return result;

    }


    @PostMapping("/getAticleByid/{id}")
    @ResponseBody
    public MyResult getAticleByID(@PathVariable("id") int id) {
        Article article = myService.getArticleByid(id);
        MyResult result = new MyResult(0, "请求成功", 1, article);
        return result;

    }


    @PostMapping("/updateArticle")
    @ResponseBody
    public MyResult updateArticle(Article article) {
        Map<String, Object> map = new HashMap<>();
        Integer re = myService.updateArticle1(article);
        MyResult result = new MyResult(0, "请求成功", 1, re);
        return result;

    }


    @GetMapping("deleteAByids")
    @ResponseBody
    public Map<String, Object> deleteAByids(Integer[] ids) {

        Map<String, Object> map = new HashMap<>();
        Integer re = null;
        // for (Integer i : ids) {
        re = myService.DeleteArticleByids(ids);
        //  }
        map.put("code", 0);
        map.put("msg", "请求成功");
        map.put("count", 1);//总记录数
        map.put("data", re);
        return map;
    }

//
//    @PostMapping("searchByWhere")
//    @ResponseBody
//    public Map<String, Object> searchByWhere(Article article) {
//
//        Map<String, Object> map = new HashMap<>();
//        Integer re = null;
//        // for (Integer i : ids) {
//        List<Article> reArticles = myService.searchByWhere(article);
//        //  }
//        map.put("code", 0);
//        map.put("msg", "查询成功");
//        map.put("count", 1);//总记录数
//        map.put("data", reArticles);
//        return map;
//    }


}
